package com.eatme.web.app.coupon.template.controller;

import com.alibaba.fastjson2.JSON;
import com.eatme.web.app.coupon.common.exception.AppException;
import com.eatme.web.app.coupon.common.vo.CouponTemplateSDK;
import com.eatme.web.app.coupon.template.entity.CouponTemplate;
import com.eatme.web.app.coupon.template.service.IBuildTemplateService;
import com.eatme.web.app.coupon.template.service.ITemplateBaseService;
import com.eatme.web.app.coupon.template.vo.CouponTemplateRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 优惠券模版相关功能控制器
 */
@RestController
public class CouponTemplateController {

    private static final Logger LOG = LoggerFactory.getLogger(CouponTemplateController.class);

    /** 构建优惠券模版服务*/
    private final IBuildTemplateService buildTemplateService;

    /** 优惠券模版基础服务*/
    private final ITemplateBaseService templateBaseService;


    public CouponTemplateController(IBuildTemplateService buildTemplateService, ITemplateBaseService templateBaseService) {
        this.buildTemplateService = buildTemplateService;
        this.templateBaseService = templateBaseService;
    }

    /**
     * 构建优惠券模版
     * @param request
     * @return
     * @throws AppException
     */
    @PostMapping("template/build")
    public CouponTemplate buildTemplate(@RequestBody CouponTemplateRequest request) throws AppException {
        LOG.info("Bulid Template:{}", JSON.toJSONString(request));
        return buildTemplateService.buildTemplate(request);
    }

    /**
     * 构造优惠券模版详情
     * @param id
     * @return
     * @throws AppException
     */
    @GetMapping("/template/info")
    public CouponTemplate buildTemplateInfo(Integer id) throws AppException {
        LOG.info("Bulid Template Info For:{}", id);
        return  templateBaseService.buildTemplateInfo(id);
    }

    /**
     * 查找所有可用的优惠券模版
     * @return
     * @throws AppException
     */
    @GetMapping("/template/sdk/all")
    public List<CouponTemplateSDK> findAllUsableTemplate() throws AppException {
        LOG.info("Find All UsableTemplate.");
        return templateBaseService.findAllUsableTemplate();
    }

    /**
     * 获取模版 ids 到 CouponTemplateSDK 的映射
     * @param ids
     * @return
     */
    @GetMapping("/template/sdk/infos")
    public Map<Integer,CouponTemplateSDK> findIds2TemplateSDK(@RequestParam("ids") Collection<Integer> ids) {
        LOG.info("findIds2TemplateSDK:{}",JSON.toJSONString(ids));
        return templateBaseService.findIds2TemplateSDK(ids);
    }
}
